package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.CommunityDAO;
import com.daxia.ogmall.dto.CommunityDTO;
import com.daxia.ogmall.model.City;
import com.daxia.ogmall.model.Community;
import com.daxia.ogmall.model.District;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class CommunityService {
	
	@Autowired
	private CommunityDAO communityDAO;
	
	public List<CommunityDTO> find(CommunityDTO query, Page page) {
		List<Community> models = communityDAO.find(query, page);
		List<CommunityDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<CommunityDTO> findAll() {
		return this.find(new CommunityDTO(), null);
	}
	
	public List<CommunityDTO> findByCityId(Long cityId){
		City city=new City();
		city.setId(cityId);
		CommunityDTO query=new CommunityDTO();
		query.setCity(city);
		return this.find(query, null);
	}
	
	public List<CommunityDTO> findByCityAndDistrictId(Long cityId,Long districtId){
		City city=new City();
		city.setId(cityId);
		District district=new District();
		district.setId(districtId);
		CommunityDTO query=new CommunityDTO();
		query.setCity(city);
		query.setDistrict(district);
		return this.find(query, null);
	}
	
	public List<CommunityDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new CommunityDTO(), page);
	}
	
	public Long create(CommunityDTO dto) {
		Community model = new Community();
		toModel(model, dto);
		return communityDAO.create(model);
	}
	
	public CommunityDTO load(Long id) {
	    Community model = communityDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(CommunityDTO dto) {
		Community model = communityDAO.load(dto.getId());
		toModel(model, dto);
		communityDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				communityDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public CommunityDTO findOne(CommunityDTO query) {
		Community model = communityDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<CommunityDTO> toDTOs(List<Community> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<CommunityDTO>(0);
		}
		List<CommunityDTO> dtos = new ArrayList<CommunityDTO>(models.size());
		for (Community model : models) {
	        CommunityDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private CommunityDTO toDTO(Community model) {
		if (model == null) {
			return null;
		}
		CommunityDTO dto = BeanMapper.map(model, CommunityDTO.class);
		
		return dto;
	}
	
	private void toModel(Community model, CommunityDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<Community> toModels(List<CommunityDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<Community>(0);
		}
		List<Community> models = new ArrayList<Community>(dtos.size());
		for (CommunityDTO dto : dtos) {
	        Community model = new Community();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
